var childWindow = new Array();
//var eDatMain;

/*
/*
/*
 * routine for forming Main Footer/Header links to help
 */
function formMainHeaderFooter(helpUrl, helpUrlAddOnEdat, helpUrlAddOnContactUs) {
	var headerHelpLink = document.getElementById('helpUrlMainHeader');
	var footerHelpLink = document.getElementById('helpUrlMainFooter');
	var headerHelpLinkAddOnEdat = document.getElementById('helpUrlMainHeaderAddOnEdat');
	var footerHelpLinkAddOnEdat = document.getElementById('helpUrlMainFooterAddOnEdat');
	var headerHelpLinkAddOnContactUs = document.getElementById('helpUrlMainHeaderAddOnContactUs');
	var footerHelpLinkAddOnContactUs = document.getElementById('helpUrlMainFooterAddOnContactUs');
	var onClickActionHelpLink = _formMainHeaderFooter(helpUrl);
	var onClickActionHelpLinkAddOnEdat = _formMainHeaderFooter(helpUrlAddOnEdat);
	var onClickActionHelpLinkAddOnContactUs = _formMainHeaderFooter(helpUrlAddOnContactUs);
	headerHelpLink.onclick = onClickActionHelpLink;
	footerHelpLink.onclick = onClickActionHelpLink;
	headerHelpLinkAddOnEdat.onclick = onClickActionHelpLinkAddOnEdat;
	footerHelpLinkAddOnEdat.onclick = onClickActionHelpLinkAddOnEdat;
	headerHelpLinkAddOnContactUs.onclick = onClickActionHelpLinkAddOnContactUs;
	footerHelpLinkAddOnContactUs.onclick = onClickActionHelpLinkAddOnContactUs;
}

/*
 * local usage ONLY
 */
function _formMainHeaderFooter(helpUrl) {
	var result = function () {
		eval ("openHelpWND('" + helpUrl + "');");
		return false;
	};
	return result;
}

function openWindow(url, targetWindow) {

	   if ( childWindow[targetWindow] && childWindow[targetWindow].open && !childWindow[targetWindow].closed ) {
	      childWindow[targetWindow].focus();
	      }
	   else {
	      childWindow[targetWindow] = window.open(url, targetWindow, "menubar=1,resizable=1,scrollbars=1,status=yes,width=1050,height=820");
	   }

	   return childWindow[targetWindow];
	}

function openWindowNew(url, targetWindow) {
	var firstTest = targetWindow + "0";
	var secondTest = targetWindow + "1";
	var openedWindow;
	if (isChildWindowOpened(firstTest)) {
		// first window is open
		if (isChildWindowOpened(secondTest)) {
			// second window open --> focus to first window
			openedWindow = firstTest;
		} else {
			// second window is closed --> open second window
			windowOpenHelper(url, secondTest);
			openedWindow = secondTest;
		}
	} else {
		// first window is not open --> open first window
		windowOpenHelper(url, firstTest);
		openedWindow = firstTest;
	}
	childWindow[openedWindow].focus();
	return childWindow[openedWindow];
}

function openEquipmentPartUsage(programModelListId) {
	var obj = document.getElementById(programModelListId);

	if (obj[obj.selectedIndex].text == '787') {
		openWindowNew('CatiaMapper.jspx', 'EquipPartUsageWindow787');
	} else {
		openWindowNew('EquipPartUsage.jspx', 'EquipPartUsageWindowNon787');
	}
}

function openGenerateHarness(programModelListId) {
	var obj = document.getElementById(programModelListId);

	if (obj[obj.selectedIndex].text == '787') {
		openWindow('PHGWireBundle.jspx', 'PHGWindow');
	} else {
		alert("Generate Harness Data is not available for the "
				+ obj[obj.selectedIndex].text + " program at this time.");
	}
}
function openGenerateInstallation(programModelListId) {
	var obj = document.getElementById(programModelListId);

	if (obj[obj.selectedIndex].text == '787') {
		openWindow('PHGInstallation.jspx', 'PHGInstallationWindow');
	} else {
		alert("Generate Installation Data is not available for the "
				+ obj[obj.selectedIndex].text + " program at this time.");
	}
}


function windowOpenHelper(url, targetWindow) {
    childWindow[targetWindow] = window.open(url, targetWindow, "menubar=1,resizable=1,scrollbars=1,status=yes,width=1280,height=1024");
}
function createTagetChileName(jspxName, callerName){
	var originalName = callerName.substring(0, callerName.length - 1);
	var originalSuffix = callerName.substring(callerName.length - 1);
	var testedSuffix = +originalSuffix;
	testedSuffix += 1;
	testedSuffix %= 2;
	var testedName = originalName + testedSuffix;
        return testedName;
}
function getWindow(testedName){
    return childWindow[testedName];
}
function setWindow(testedName, childWindow){
    childWindow[testedName]=childWindow;
}
function openSecondWindow(jspxName, callerName) {
    var originalName = callerName.substring(0, callerName.length - 1);
    var originalSuffix = callerName.substring(callerName.length - 1);
    var testedSuffix = +originalSuffix;
    testedSuffix += 1;
    testedSuffix %= 2;
    var testedName = originalName + testedSuffix;
    if (!isChildWindowOpened(testedName)) {
        windowOpenHelper(jspxName, testedName);
    }
    //childWindow[testedName].focus();
    return childWindow[testedName];
}

function isChildWindowOpened(wndName) {
	return childWindow[wndName] && childWindow[wndName].open && !childWindow[wndName].closed;
}

function closeAllChildWindow() {

   for (var i in childWindow) {
      if ( childWindow[i] && childWindow[i].open && !childWindow[i].closed ) {
         childWindow[i].close();
         childWindow[i] = null;
      }
   }
}

function openPHGWireBundleWindow(url, targetWindow){
	var UrlPlusApModel = url+"?airplaneModel="+document.getElementById('lstDefaultModel')[document.getElementById('lstDefaultModel').selectedIndex].text;
	return openWindow(UrlPlusApModel, targetWindow);
}


function logOff() {
   var confirmation = window.confirm('You are logging off FIODS applications.  Please click OK to confirm.');
   /*
   if ( confirmation ) {
      closeAllChildWindow();
      return true;
   }
  */

   return confirmation;
}


/*
function checkModelSelection(obj) {
   //childModelField = childWindow["HarnessWindow"].document.getElementById("txtModel");
   //childModelField.value = obj.value;

   if ( obj.value == '737' || obj.value=='747') {
      alert("Currently there are no data for " + obj.value + " model.")
      obj.value = "787";
   }
}
*/

function openHelpPageWindow(url, targetWindow) {
   var options = "menubar=1,toolbar=1,resizable=1,scrollbars=1,status=yes,width=1050,height=720";


   childWindow[targetWindow] = window.open( helpServerPrefix + "blank.html"
                                          , targetWindow
                                          , options
                                          );


   childWindow[targetWindow] = window.open( url
                                          , targetWindow
                                          , options
                                          );

   childWindow[targetWindow].focus();
   return childWindow[targetWindow];
}


function openHelpWindow(url) {
   var targetWindow = openHelpPageWindow(helpServerPrefix + url, "HELP");

   if (targetWindow) targetWindow.focus();
   return false;
}


function openNoPrefixHelpWindow(url) {
   var targetWindow = openHelpPageWindow(url, "HELP");

   if (targetWindow) targetWindow.focus();
   return false;
}

function disableAllFunctions(){
	document.getElementById("tdBundleCompare").className = "disabledColor";
	document.getElementById("tdBundleCompare").disabled = true;
	document.getElementById("tdBundleCompare").onclick = null;
	document.getElementById("tdWBAG").className = "disabledColor";
	document.getElementById("tdWBAG").disabled = true;
	document.getElementById("tdWBAG").onclick = null;
	document.getElementById("tdBundleSearch").className = "disabledColor";
	document.getElementById("tdBundleSearch").disabled = true;
	document.getElementById("tdBundleSearch").onclick = null;
	document.getElementById("tdBundleDrawingAid").className = "disabledColor";
	document.getElementById("tdBundleDrawingAid").disabled = true;
	document.getElementById("tdBundleDrawingAid").onclick = null;
	document.getElementById("tdTemi").className = "disabledColor";
	document.getElementById("tdTemi").disabled = true;
	document.getElementById("tdTemi").onclick = null;
	document.getElementById("tdDictNotes").className = "disabledColor";
	document.getElementById("tdDictNotes").disabled = true;
	document.getElementById("tdDictNotes").onclick = null;
	document.getElementById("tdHarnessTermData").className = "disabledColor";
	document.getElementById("tdHarnessTermData").disabled = true;
	document.getElementById("tdHarnessTermData").onclick = null;
	document.getElementById("tdCatiaModelAnalysisCheck").className = "disabledColor";
	document.getElementById("tdCatiaModelAnalysisCheck").disabled = true;
	document.getElementById("tdCatiaModelAnalysisCheck").onclick = null;
	document.getElementById("tdCatiaMapper").className = "disabledColor";
	document.getElementById("tdCatiaMapper").disabled = true;
	document.getElementById("tdCatiaMapper").onclick = null;
	document.getElementById("tdPHGWireBundle").className = "disabledColor";
	document.getElementById("tdPHGWireBundle").disabled = true;
	document.getElementById("tdPHGWireBundle").onclick = null;
	document.getElementById("tdPHGInstallation").className = "disabledColor";
	document.getElementById("tdPHGInstallation").disabled = true;
	document.getElementById("tdPHGInstallation").onclick = null;
	document.getElementById("tdPHGInstallation").className = "disabledColor";
	document.getElementById("tdPHGInstallation").disabled = true;
	document.getElementById("tdPHGInstallation").onclick = null;
	document.getElementById("tdModelType").className = "disabledColor";
	document.getElementById("tdModelType").disabled = true;
	document.getElementById("tdModelType").onclick = null;
}

function checkModelSelection(objId) {

	var obj = document.getElementById(objId);

	// just disable all functions and then turn on the airplane model specific functions.
	disableAllFunctions();

	if (obj[obj.selectedIndex].text == "787") {

		document.getElementById("tdHarnessTermData").disabled = false;
		document.getElementById("tdHarnessTermData").className = "MainMenuItem";
		document.getElementById("tdHarnessTermData").onclick = function() {
			openWindow('HarnessQuery.jsp', 'HarnessWindow');
		};
		document.getElementById("tdCatiaModelAnalysisCheck").className = "MainMenuItem";
		document.getElementById("tdCatiaModelAnalysisCheck").disabled = false;
		document.getElementById("tdCatiaModelAnalysisCheck").onclick = function() {
			processKbeClick('lstDefaultModel');
		};
		document.getElementById("tdCatiaMapper").disabled = false;
		document.getElementById("tdCatiaMapper").className = "MainMenuItem";
		document.getElementById("tdCatiaMapper").onclick = function() {
			openEquipmentPartUsage('lstDefaultModel');
		};
		document.getElementById("tdPHGWireBundle").disabled = false;
		document.getElementById("tdPHGWireBundle").className = "MainMenuItem";
		document.getElementById("tdPHGWireBundle").onclick = function() {
			openPHGWireBundleWindow('PHGWireBundle.jspx', 'PHGWindow');
		};
		document.getElementById("tdPHGInstallation").disabled = false;
		document.getElementById("tdPHGInstallation").className = "MainMenuItem";
		document.getElementById("tdPHGInstallation").onclick = function() {
			openPHGWireBundleWindow('PHGInstallation.jspx', 'PHGInstallationWindow');
		};
		document.getElementById("tdModelType").disabled = false;
		document.getElementById("tdModelType").className = "MainMenuItem";
		document.getElementById("tdModelType").onclick = function() {
			openWindow('ModelType.jspx', 'ModelTypeWindow');
		};

	} else if (obj[obj.selectedIndex].text != null &&  obj[obj.selectedIndex].text != ''){
		document.getElementById("tdBundleCompare").disabled = false;
		document.getElementById("tdBundleCompare").className = "MainMenuItem";
		document.getElementById("tdBundleCompare").onclick = function() {
			openWindow('BundleCompare.jspx', 'BundleCompareWindow');
		};
		document.getElementById("tdWBAG").disabled = false;
		document.getElementById("tdWBAG").className = "MainMenuItem";
		document.getElementById("tdWBAG").onclick = function() {
			openWindow('WBAG.jspx', 'WBAGWindow');
		};
		document.getElementById("tdBundleSearch").disabled = false;
		document.getElementById("tdBundleSearch").className = "MainMenuItem";
		document.getElementById("tdBundleSearch").onclick = function() {
			openWindow('BundleSearch.jspx', 'BundleSearchWindow');
		};
		document.getElementById("tdBundleDrawingAid").disabled = false;
		document.getElementById("tdBundleDrawingAid").className = "MainMenuItem";
		document.getElementById("tdBundleDrawingAid").onclick = function() {
			openWindow('BundleDrawingAid.jspx', 'BundleDrawingAidWindow');
		};
		document.getElementById("tdTemi").disabled = false;
		document.getElementById("tdTemi").className = "MainMenuItem";
		document.getElementById("tdTemi").onclick = function() {
			openWindow('Temi.jspx', 'TemiWindow');
		};
		document.getElementById("tdDictNotes").disabled = false;
		document.getElementById("tdDictNotes").className = "MainMenuItem";
		document.getElementById("tdDictNotes").onclick = function() {
			openWindow('DictNotes.jspx', 'DictNotesEquipWindow');
		};
		document.getElementById("tdCatiaModelAnalysisCheck").className = "MainMenuItem";
		document.getElementById("tdCatiaModelAnalysisCheck").disabled = false;
		document.getElementById("tdCatiaModelAnalysisCheck").onclick = function() {
			processKbeClick('lstDefaultModel');
		};
		document.getElementById("tdCatiaMapper").disabled = false;
		document.getElementById("tdCatiaMapper").className = "MainMenuItem";
		document.getElementById("tdCatiaMapper").onclick = function() {
			openEquipmentPartUsage('lstDefaultModel');
		};
		document.getElementById("tdPHGWireBundle").disabled = false;
		document.getElementById("tdPHGWireBundle").className = "MainMenuItem";
		document.getElementById("tdPHGWireBundle").onclick = function() {
			openPHGWireBundleWindow('PHGWireBundle.jspx', 'PHGWindow');
		};
		document.getElementById("tdPHGInstallation").disabled = false;
		document.getElementById("tdPHGInstallation").className = "MainMenuItem";
		document.getElementById("tdPHGInstallation").onclick = function() {
			openPHGWireBundleWindow('PHGInstallation.jspx', 'PHGInstallationWindow');
		};

	}

}


function isIEBrowser() {

   if ( navigator.appName != "Microsoft Internet Explorer" ) {
      alert( "This menu item is only available when using Microsoft Internet Explorer." );
      return false;
   }

   return true;
}


function processKbeClick(modelListId) {
	var obj = document.getElementById(modelListId);

	if(obj[obj.selectedIndex].text != '787'){
		alert("CATIA Model Analysis & Checks is not available for " + obj[obj.selectedIndex].text + " program at this time.");
		return;
	}

   if ( isIEBrowser() ) {
      if (document.getElementById("hidAccessAppFromField").value == "PORTAL") {
         kbeServerPath = kbePortalServerPath;
      }
      else if (document.getElementById("hidAccessAppFromField").value == "INTERNAL") {
         kbeServerPath = kbeInternalServerPath;
      }
      openWindow(kbeServerPath, 'KBEWindow');
   }
}

function ReadFile(filename)
{


   var ForReading = 1, ForWriting = 2, ForAppending = 8;
   var TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0;

   var fso = new ActiveXObject("Scripting.FileSystemObject");

   // Open the file for output.
   //var filename = ;

   //var f = fso.OpenTextFile(filename, ForWriting, true);
   // Write to the file.
   //f.WriteLine("Hello world!");
   //f.WriteLine("The quick brown fox");
   //f.Close();

   // Open the file for input.
   f = fso.OpenTextFile(filename, ForReading);

   if(!fso.FileExists()){
	   alert(filename + " does not exist.")
	   f.Close();
	   return;
   }

   // Read from the file and display the results.
   alert(filename + " reading...")
   while (!f.AtEndOfStream)
       {
       var r = f.ReadLine();
       //document.write (r + "<br />");
       }
   alert(filename + " read complete.")
	f.Close();

}

function processHelpLink(){
	var fileName = edatHelpFileProperty;
	ReadFile(fileName);
}
//function storeMainWindow(mainWindow){
//    eDatMain=mainWindow;
//}